------------------------------------------------------------------------------------------------------------------------------------
      name:  plog_521
       log:  /accounts/projects/jr_ra/GRscarring/erratum/programs/prepare/findpartners.log
  log type:  text
 opened on:  27 Nov 2024, 15:44:04

. * findpartners.do
. * JR 8/1/2017
. * 
. * This file identifies households in the CPS that have people of the opposite 
. *       gender and are within a 10 year age range of one another. 
. *       This is used in /extractcps.do to identify people living with a partner 
. * As written this relies on data files in ~/data/cps/bigcps/statafmt,
. *
. * Edited by JR, 9/22/17: Rename a variable at the end, clean up a couple other minor issues
. * Edited by JR, 10/24/17: Redo code to find why it was failing, and simplify greatly.
. *   Previous problem was that code assumed sex was 1/2, when the input data set had it as 0/1.
. 
. cap project, doinfo

. if _rc==0 {
.   local doasproject=1
.   local pdir "`r(pdir)'"                                                            // the project's main dir.
.   local dofile "`r(dofile)'"                                                // do-file's stub name
.   local sig {bind:{hi:[RP : `dofile'.do]}}      // a signature in notes
. }

. else {
.   local doasproject=0
.   di "RUNNING OUTSIDE OF PROJECT"
.   local pdir "~/GRscarring"
.   local dofile "findpartners"
.   local sig "Not run as part of project!"
. }

. 
. local cpsorig "`pdir'/rawdata/readrawcps/bigcps"

. local intermediate "`pdir'/scratch"

. 
. set more off

. *set trace on
. program drop _all

. set type double, perm
(set type preference recorded)

. #delimit ;
delimiter now ;
. if `doasproject'==1 {;
.   project, uses("`intermediate'/extractcps.dta.gz");
project GRscar_erratum > do-file uses: "/scratch/public/jr_ra/GRscarring2024/erratum/scratch/extractcps.dta.gz" filesig(1117482818:1
> 425161591)
. };

. *unzip extractcps;
. !zcat `intermediate'/extractcps.dta.gz > `intermediate'/extractcps.dta;


.  *use if year==2013 using `intermediate'/extractcps.dta;
. use `intermediate'/extractcps, clear;

. sort yearmo hh_id hh_num hh_tiebreak linenum p_tiebreak;

. isid yearmo hh_id hh_num hh_tiebreak linenum p_tiebreak;

. *Make a temporary household and person ID to save typing;
.  egen temp_hhid=group(yearmo hh_id hh_num hh_tiebreak);

.  egen temp_pid=group(temp_hhid linenum p_tiebreak);

.  sort temp_hhid temp_pid;

.  tempfile base;

.  save `base';
file /tmp/St2868349.000004 saved as .dta format

.  *Get list of all mens and womens ages in the household;
.   gen man=(sex==1);

.   gen woman=(sex==0);

.   by temp_hhid: gen mannum=sum(man);

.   by temp_hhid: gen womnum=sum(woman);

.   gen num=mannum*man + womnum*woman;

.   su num, meanonly;

.   local maxnum=r(max);

.   keep temp_hhid sex num age;

.   reshape wide age, i(temp_hhid sex) j(num);
(j = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22)

Data                               Long   ->   Wide
-----------------------------------------------------------------------------
Number of observations       52,432,999   ->   43,608,311  
Number of variables                   4   ->   24          
j variable (22 values)              num   ->   (dropped)
xij variables:
                                    age   ->   age1 age2 ... age22
-----------------------------------------------------------------------------

.   gen listages="";
(43,608,311 missing values generated)

.   foreach v of varlist age* {;
  2.     replace listages=listages+ " " + string(`v') if `v'<.;
  3.   };
variable listages was str1 now str3
(43,608,311 real changes made)
variable listages was str3 now str6
(7,268,013 real changes made)
variable listages was str6 now str9
(1,304,446 real changes made)
variable listages was str9 now str12
(205,630 real changes made)
variable listages was str12 now str15
(34,968 real changes made)
variable listages was str15 now str18
(7,924 real changes made)
variable listages was str18 now str21
(2,161 real changes made)
variable listages was str21 now str24
(832 real changes made)
variable listages was str24 now str27
(323 real changes made)
variable listages was str27 now str30
(161 real changes made)
variable listages was str30 now str33
(88 real changes made)
variable listages was str33 now str36
(60 real changes made)
variable listages was str36 now str39
(37 real changes made)
variable listages was str39 now str42
(24 real changes made)
variable listages was str42 now str45
(6 real changes made)
variable listages was str45 now str48
(3 real changes made)
variable listages was str48 now str51
(3 real changes made)
variable listages was str51 now str54
(2 real changes made)
variable listages was str54 now str57
(2 real changes made)
variable listages was str57 now str60
(2 real changes made)
variable listages was str60 now str63
(2 real changes made)
variable listages was str63 now str66
(1 real change made)

.   keep temp_hhid sex listages;

.   *Convert men to women and vice versa to match to opposite sex;
.    replace sex=1-sex;
(43,608,311 real changes made)

.   tempfile listages;

.   save `listages';
file /tmp/St2868349.000005 saved as .dta format

. *Identify opposite sex partners within 10 years;
.  use `base';

.  merge m:1 temp_hhid sex using `listages', keep(1 3) nogen;
(label sex_label already defined)

    Result                      Number of obs
    -----------------------------------------
    Not matched                    11,515,235
        from master                11,515,235  
        from using                          0  

    Matched                        40,917,764  
    -----------------------------------------

.  gen byte haspartner=0;

.  forvalues i=1/`maxnum' {;
  2.    gen testage=real(word(listages, `i'));
  3.    replace haspartner=1 if testage<. & abs(age-testage)<=10;
  4.    drop testage;
  5.  };
(11,515,235 missing values generated)
(30,002,895 real changes made)
(44,290,545 missing values generated)
(2,267,156 real changes made)
(50,862,091 missing values generated)
(78,932 real changes made)
(52,169,911 missing values generated)
(8,398 real changes made)
(52,385,332 missing values generated)
(1,051 real changes made)
(52,422,113 missing values generated)
(222 real changes made)
(52,430,457 missing values generated)
(30 real changes made)
(52,432,153 missing values generated)
(5 real changes made)
(52,432,746 missing values generated)
(2 real changes made)
(52,432,908 missing values generated)
(0 real changes made)
(52,432,956 missing values generated)
(0 real changes made)
(52,432,978 missing values generated)
(1 real change made)
(52,432,995 missing values generated)
(0 real changes made)
(52,432,998 missing values generated)
(0 real changes made)
(52,432,999 missing values generated)
(0 real changes made)
(52,432,999 missing values generated)
(0 real changes made)
(52,432,999 missing values generated)
(0 real changes made)
(52,432,999 missing values generated)
(0 real changes made)
(52,432,999 missing values generated)
(0 real changes made)
(52,432,999 missing values generated)
(0 real changes made)
(52,432,999 missing values generated)
(0 real changes made)
(52,432,999 missing values generated)
(0 real changes made)

.   //Combine marital status with has partner.;
>  gen byte lives_spouse_oth=haspartner;

.  replace lives_spouse_oth=1 if married==1;
(2,672,003 real changes made)

.  //What about people who live with their parents and younger siblings? Seems best to not
>  //count unmarried partners of people who live with their parents;
>   replace lives_spouse_oth=0 if livewithprnt==1 & married~=1;
(2,068,422 real changes made)

.   *Now save a file;
.  keep yearmo hh_id hh_num hh_tiebreak linenum p_tiebreak haspartner lives_spouse_oth;

.  save `intermediate'/`dofile', replace;
(file /accounts/projects/jr_ra/GRscarring/erratum/scratch/findpartners.dta not found)
file /accounts/projects/jr_ra/GRscarring/erratum/scratch/findpartners.dta saved

.  ! gzip -f `intermediate'/`dofile'.dta;


.  !rm `intermediate'/`dofile'.dat;

rm: cannot remove '/accounts/projects/jr_ra/GRscarring/erratum/scratch/findpartners.dat': No such file or directory

. if `doasproject'==1 project, creates("`intermediate'/`dofile'.dta.gz");
project GRscar_erratum > do-file creates: "/scratch/public/jr_ra/GRscarring2024/erratum/scratch/findpartners.dta.gz" filesig(3070793
> 94:268425751)

. !rm `intermediate'/extractcps.dta;


. 
end of do-file
      name:  plog_521
       log:  /accounts/projects/jr_ra/GRscarring/erratum/programs/prepare/findpartners.log
  log type:  text
 closed on:  27 Nov 2024, 16:12:25
------------------------------------------------------------------------------------------------------------------------------------
